package com.carambatv.android.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;

import com.carambatv.android.db.CarambaDBSchema;
import com.carambatv.android.db.ChannelColumns;
import com.carambatv.android.model.ChannelObject;

public class ChannelProcessor {
	public static final String ENTRY = "entry";
	public static final String ID = "id";
	public static final String FIRSTNAME = "firstName";
	public static final String STATISTICS = "statistics";
	public static final String THUMBNAIL = "thumbnail";
	public static final String URL = "url";
	public static final String SUBSCRIBERCOUNT = "subscriberCount";
	public static final String FEED_LINK = "feedLink";
	public static final String COUNT_HINT = "countHint";
	public static final String HREF = "href";

	private String thumbnail;
//	private String title = null;
	private String total = null;
	private String subscribers = null;

	public void processInternal(final ContentResolver resolver, final InputStream in, String type) throws IOException, HttpException, Exception {
		ArrayList<ContentProviderOperation> insertOperations = new ArrayList<ContentProviderOperation>();
		try {

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(in, "UTF-8");

			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(ENTRY)) {
					thumbnail = null;
//					title = null;
				}

//				else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(FIRSTNAME)) {
//					eventType = xpp.next();
//					title = xpp.getText();
//				} 
				else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(STATISTICS)) {
					int size = xpp.getDepth();
					for (int i = 0; i < size; i++) {
						String attrName = xpp.getAttributeName(i);
						String attrValue = xpp.getAttributeValue(i);

						if (attrName != null && attrName.equals(SUBSCRIBERCOUNT)) {
							subscribers = attrValue;
						}
					}
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(THUMBNAIL)) {
					int size = xpp.getDepth();
					for (int i = 0; i < size; i++) {
						String attrName = xpp.getAttributeName(i);
						String attrValue = xpp.getAttributeValue(i);

						if (attrName != null && attrName.equals(URL)) {
							thumbnail = attrValue;
							break;
						}
					}
				} else if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(FEED_LINK)) {
					int size = xpp.getDepth();
					for (int i = 0; i < size; i++) {
						String attrName = xpp.getAttributeName(i);
						String attrValue = xpp.getAttributeValue(i);

						if (attrName != null && attrName.equals(HREF) && attrValue.endsWith("uploads")) {
							String attrName_next = xpp.getAttributeName(i + 1);
							String attrValue_next = xpp.getAttributeValue(i + 1);
							if (attrName_next != null && attrName_next.equals(COUNT_HINT) && attrValue_next != null) {
								total = attrValue_next;
								break;
							}

						}

						// if (attrName != null && attrName.equals(HEIGHT) &&
						// attrValue != null && attrValue.equals(i360)) {
						// break;
						// }
					}
				}

				else if (eventType == XmlPullParser.END_TAG && xpp.getName().equals(ENTRY)) {
					insertOperations.add(ContentProviderOperation.newUpdate(ChannelObject.CONTENT_URI)
							.withSelection(ChannelColumns.ID_CHANNEL.getName() + "=?", new String[] { type })
							.withValue(ChannelColumns.SUBSCRIBERS.getName(), subscribers)
							.withValue(ChannelColumns.THUMBNAIL.getName(), thumbnail)
							.withValue(ChannelColumns.TOTAL.getName(), total)
							.build());
				}
				eventType = xpp.next();
			}
		} finally {
			IOUtil.closeQuietly(in);
		}
		resolver.applyBatch(CarambaDBSchema.CONTENT_AUTHORITY, insertOperations);
	}
}
